package interceptor;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
/**
 * 过滤方法
 *
 */
public class MethodFilter extends MethodFilterInterceptor{

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		System.out.println("---日志记录---");
		String name="tom";//可以从session中获取
		String msg=name+"在"+new Date()+"做了"+invocation.getProxy().getMethod()+"操作";//invocation.getProxy().getMethod()方法名
		//写入日志文件
		recordLog(msg);
		return invocation.invoke();
	}

	private void recordLog(String msg) throws IOException {
		PrintWriter writer=new PrintWriter(new FileWriter("/home/tarena/MethodFilterLog.txt", true));
		writer.println(msg);
		writer.close();
	}

}
